package UFFT_package;

import java.io.File;
import java.io.FileWriter;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
// Notice, do not import com.mysql.jdbc.*
// or you will have problems!
import java.sql.Statement;

public class connect 
{
	static Connection conn=null;
	static ResultSet res,rs;
	static File file;
	static PrintWriter pw;
	static FileWriter fw;
	static Statement st,st1;
	static String temp,str;
	static int numClasses=0;
	static int temp1=0;
	static int numDimensions=0;
	static int numExamples=0;
	
    public static void connect1()
    {
        try 
        {
            // The newInstance() call is a work around for some
            // broken Java implementations
            Class.forName("com.mysql.jdbc.Driver").newInstance();
           // conn=DriverManager.getConnection("jdbc:mysql://localhost/tennis","root","");
            conn=DriverManager.getConnection("jdbc:mysql://localhost/car","root","");
            try
            {
                //file=new File("D:\\input.txt");
                file=new File("D:\\input1.txt");
                fw=new FileWriter(file);
                pw = new PrintWriter(fw);
                
                /*---------------------------For Classes.----------------------*/
                pw.print("classes ");
                st = conn.createStatement();
                st1=conn.createStatement();
                res = st.executeQuery("SELECT distinct(class_label) FROM  car1");
                
                while(res.next())
                {
                	numClasses++;
                }
                res.close();
                pw.print(numClasses + " ");
                res = st.executeQuery("SELECT distinct(class_label) FROM  car1");
                while(res.next())
                {
                	temp=res.getString("class_label");
                	pw.print(temp + " ");
                }                
                res.close();
                
                /*----------------------------For Dimensions---------------------*/
                pw.println();
                pw.print("dimensions ");
                res=st.executeQuery("Select * from information_schema.columns where table_name='car1'");
                res.last();
                numDimensions=res.getRow();
                res.close();
                                
                pw.print((numDimensions-1)+" ");
                res=st.executeQuery("Select * from information_schema.columns where table_name='car1'");
                System.out.println("123");
                temp1=1;
	            
                while(res.next() && temp1<numDimensions)
	            {	
	            	temp=res.getString("column_name");
	            	str="select distinct("+temp+") from car1";
	            	rs=st1.executeQuery(str);
	            	rs.last();
	            	int pos_values=rs.getRow();
	            	System.out.println(str+" "+pos_values);
	            	rs.close();
	            	pw.print(temp + " " + pos_values+ " ");
	            	
	        /*----------------------add possible values for each attribute------------------*/
	            	str="select distinct("+temp+") as 'Values' from car1";
	            	rs=st1.executeQuery(str);
	            	while(rs.next())
	            	{
	            		temp=rs.getString("Values");
	            		System.out.println(temp);
	            		pw.print(temp+" ");
	            	}
	            	
	                temp1++;
	                rs.close();
	            }	
	            
                res.close();
                System.out.println("before examples");
                /* --------------------For Examples------------------------------ */
                pw.println();
                pw.print("Examples ");
                res=st.executeQuery("Select * from car1");
                res.last();
                numExamples=res.getRow();
                res.close();
                
                res=st.executeQuery("Select * from car1");
                res.close();
                pw.print(numExamples + " ");
                res = st.executeQuery("SELECT * FROM  car1");
                while(res.next())
                {
                	int t1=1;
                	pw.println();
                	while(t1<=numDimensions)
                	{
                		temp=res.getString(t1);
                		pw.print(temp+"  ");
                		t1++;
                	}
                }                
                res.close();
                
                
                conn.close();
            }
            catch (SQLException s)
            {
            System.out.println("SQL code does not execute."+ s);
            }
            finally
            {
            	fw.close();
            }
        } 
        catch (Exception ex) 
        {
            ex.printStackTrace();
        }
    }
}